package org.kernely.ui.profile;

import java.io.File;

import org.kernely.security.dao.UserDetailsDAO;
import org.kernely.security.persistence.User;
import org.kernely.security.persistence.UserDetails;
import org.kernely.ui.MainApplication;
import org.kernely.ui.mvp.KernelyEvent;
import org.kernely.ui.mvp.KernelyEventReceiver;
import org.kernely.ui.mvp.KernelyPresenter;

import com.vaadin.terminal.FileResource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.themes.Reindeer;

/**
 *	Presenter of the Kernely public profile.
 */
@SuppressWarnings("serial")
public class KernelyPublicProfilePresenter extends KernelyPresenter<KernelyPublicProfileView> implements KernelyEventReceiver {

	private UserDetails userDetails;
	
	/**
	 * Constructor
	 * @param user The details of the user, which are shown.
	 */
	public KernelyPublicProfilePresenter(UserDetails userDetails){
		this.userDetails = userDetails;
	}
	
	/**
	 * Default constructor, display current user's datas.
	 */
	public KernelyPublicProfilePresenter(){
		this(UserDetailsDAO.getInstance().getUserDetails(MainApplication.getSecurityService().getCurrentUser()));
	}

	
	/**
	 * Bind method to fill fields with data.
	 */
	@Override
	public void bind() {
		
		// Listen the event bus
		MainApplication.getEventBus().addListener(this);
		
		this.view.getTitleLabel().setValue(userDetails.getFirstname()
				+" "
				+userDetails.getName()
				+"'s profile");
		String civility = null;
		switch (userDetails.getCivility()){
		case 0:
			civility = "Mr.";
			break;
		case 1:
			civility = "Mrs";
			break;
		case 2:
			civility = "Ms.";
			break;
		}
		this.view.getCivilityLabel().setValue("<span style=\"font-weight:bold;\">Civility</span> : "+civility);
		
		view.getNameLabel().setValue("<span style=\"font-weight:bold;\">Name:</span> "+userDetails.getName());
		view.getFirstnameLabel().setValue("<span style=\"font-weight:bold;\">Firstname:</span> "+userDetails.getFirstname());
		String description = userDetails.getDescription();
		if (description == null){
			view.getDescriptionLabel().setVisible(false);
		} else {
			view.getDescriptionLabel().setValue("<span style=\"font-weight:bold;\">Description: </span>"+description);
		}
		view.getEmailLabel().setValue("<span style=\"font-weight:bold;\">E-mail:</span> "+userDetails.getMail());

		String phone = userDetails.getPhone();
		if (phone == null){
			view.getPhoneLabel().setVisible(false);
		} else {
			view.getPhoneLabel().setValue("<span style=\"font-weight:bold;\">Phone number:</span> "+phone+"</span>");
		}
		
		String address = userDetails.getAddress();
		if (address == null){
			view.getAddressLabel().setVisible(false);
		} else {
			view.getAddressLabel().setValue("<span style=\"font-weight:bold;\">Address:</span> "+address);
		}
		String skype = userDetails.getSkype();
		if (skype == null){
			view.getSkypeLabel().setVisible(false);
		} else {
			view.getSkypeLabel().setValue("<span style=\"font-weight:bold;\">Skype:</span> "+skype);
		}
		String linkedin= userDetails.getLinkedin();
		if (linkedin == null){
			view.getLinkedInLabel().setVisible(false);
		} else {
			view.getLinkedInLabel().setValue("<span style=\"font-weight:bold;\">LinkedIn:</span> "+linkedin);
		}

		String twitter = userDetails.getTwitter();
		if (twitter == null){
			view.getTwitterLabel().setVisible(false);
		} else {
			view.getTwitterLabel().setValue("<span style=\"font-weight:bold;\">Twitter:</span> "+twitter);
		}

		String viadeo= userDetails.getViadeo();
		if (viadeo == null){
			view.getViadeoLabel().setVisible(false);
		} else {
			view.getViadeoLabel().setValue("<span style=\"font-weight:bold;\">Viadeo:</span> "+viadeo);
		}
		
		// Hide social networks if none is displayed
		if (viadeo == null && linkedin == null && twitter == null){
			view.getSocialTitle().setVisible(false);
			view.getSocialSeparator().setVisible(false);
		}
		
	}

	/**
	 * Method called when a KernelyEvent is received
	 */
	@Override
	public void handleKernelyEvent(KernelyEvent event) {
		if (event instanceof KernelyPublicProfileAttachedEvent){
			displayPicture();
		}
	}

	/**
	 * Display the profile's picture 
	 */
	private void displayPicture(){

		// We display the picture of the user, or a default one
		if (userDetails.getImage() == null || userDetails.getImage().equals("")){

			//The default image is located in src/main/resources/VAADIN/themes/kernelytheme/img
			if(userDetails.getCivility() == 0){
				ThemeResource imageResource = new ThemeResource("img/default_male.png");
				this.view.getPictureEmbedded().setSource(imageResource);
			}
			else{
				ThemeResource imageResource = new ThemeResource("img/default_female.png");
				this.view.getPictureEmbedded().setSource(imageResource);
			}
		} else {
			File file = new File("/tmp/uploads/"+userDetails.getImage());
			final FileResource imageResource = new FileResource(file, this.view.getApplication());
			this.view.getPictureEmbedded().setSource(imageResource);
		}

		this.view.getPictureEmbedded().requestRepaint();			

	}
	
	/**
	 * Called when the presenter is unset from UI.
	 */
	@Override
	public final void unset() {
		MainApplication.getEventBus().removeListener(this);
	}


}
